//
//  FJHomeTopView.swift
//  FruitJuice
//
//  Created by Rui on 2023/12/18.
//

import UIKit

class FJHomeTopView: UIView {

    private lazy var iconButton           = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(8).end()
    private lazy var hiLabel           = c_make_ex(UILabel.self)
        .color("#B5B5B5").font(14.font_regular).text("Hi !").end()
    private lazy var nameLabel           = c_make_ex(UILabel.self)
        .color(.black).font(17.font_regular).end()
    private lazy var msgButton           = c_make_ex(UIButton.self)
        .image("FJ_Home_message").end()
    private lazy var titleLabel           = c_make_ex(UILabel.self)
        .color(.black).font(23.font_wide).text("Homemade Drinks").end()
    private lazy var recordButton           = c_make_ex(UIButton.self)
        .image("FJ_Home_record").end()
    private lazy var popularButton           = c_make_ex(UIButton.self)
        .image("FJ_Home_popular").end()
    private lazy var recordTitleLab          = c_make_ex(UILabel.self)
        .color(.white).font(17.font_bold).text("Record").end()
    private lazy var recordDesLab          = c_make_ex(UILabel.self)
        .color(.white).font(16.font_regular).text("Every Drink You Make").end()
    private lazy var popularTitleLab          = c_make_ex(UILabel.self)
        .color(.white).font(17.font_bold).text("Popular Science").end()
    private lazy var popularDesLab          = c_make_ex(UILabel.self)
        .color(.white).font(16.font_regular).text("Juice Benefits").end()
    
    var pushToProfileVCClosure: (() -> Void)?
    var pushToMsgVCClosure: (() -> Void)?
    var pushToRecordVCClosure: (() -> Void)?
    var pushToPopularVCClosure: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        subviews{
            iconButton
            hiLabel
            nameLabel
            msgButton
            titleLabel
            recordButton.subviews{
                recordTitleLab
                recordDesLab
            }
            popularButton.subviews{
                popularTitleLab
                popularDesLab
            }
        }
        
        let w = (CGFloat.screenWidth - 40.0)/2
        iconButton.leading(15).top(.statusHeight + 15.0).size(45)
        hiLabel.leading(70).top(.statusHeight + 19.0)
        nameLabel.leading(70).trailing(210)
        nameLabel.Top == hiLabel.Bottom
        msgButton.trailing(15).width(33).height(30)
        msgButton.CenterY == iconButton.CenterY
        titleLabel.leading(15)
        titleLabel.Top == iconButton.Bottom + 15
        recordButton.leading(15).width(w).height(85)
        recordButton.Top == titleLabel.Bottom + 10
        popularButton.trailing(15).width(w).height(85)
        popularButton.Top == titleLabel.Bottom + 10
        recordTitleLab.leading(10).top(20)
        recordDesLab.leading(10).top(45)
        popularTitleLab.leading(10).top(20)
        popularDesLab.leading(10).top(45)
        
        iconButton.style { v in
            v.isUserInteractionEnabled = true
            let tap = UITapGestureRecognizer(target: self, action: #selector(pushToProfileVC))
            v.addGestureRecognizer(tap)
        }
//        iconButton.addTarget(self, action: #selector(pushToProfileVC), for: .touchUpInside)
        msgButton.addTarget(self, action: #selector(pushToMessageVC), for: .touchUpInside)
        recordButton.addTarget(self, action: #selector(pushToRecordVC), for: .touchUpInside)
        popularButton.addTarget(self, action: #selector(pushToPopularVC), for: .touchUpInside)
    }
    
    func updateUserInfo(){
        
        if let icon = USEINFOMODEL.headImage {
            self.iconButton.loadImage(url: icon)
        }else{
            self.iconButton.image = "FJ_Common_avator".toImage
        }
        if let name = USEINFOMODEL.name {
            self.nameLabel.text = name
        }else{
            self.nameLabel.text = "FruitJuice"
        }
    }
    
    @objc private func pushToProfileVC(){
        pushToProfileVCClosure?()
    }
    @objc private func pushToMessageVC(){
        pushToMsgVCClosure?()
    }
    @objc private func pushToRecordVC(){
        pushToRecordVCClosure?()
    }
    @objc private func pushToPopularVC(){
        pushToPopularVCClosure?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
